package com.github.pig.order.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import com.github.pig.order.entity.Order;
import com.github.pig.order.service.OrderService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lengleng
 * @since 2018-12-25
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController
{
    @Autowired
    private OrderService orderService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return Order
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询订单", notes = "根据ID查询订单")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "int", paramType = "path")
    public R<Order> get(@PathVariable Integer id)
    {
        return new R<>(orderService.selectById(id));
    }

    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    @ApiOperation(value = "分页查询订单", notes = "分页查询订单")
    public Page page(@RequestParam Map<String, Object> params)
    {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return orderService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param order 实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody Order order)
    {
        return new R<>(orderService.insert(order));
    }

    /**
     * 删除订单
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id)
    {
        Order order = new Order();
        order.setOrderId(id);
        order.setDelFlag("1");
        return new R<>(orderService.updateById(order));
    }

    /**
     * 编辑
     * @param order 实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody Order order)
    {
        order.setUpdateTime(new Date());
        return new R<>(orderService.updateById(order));
    }
}
